import Vue from 'vue'
import VueRouter from 'vue-router'

import sess from "@/utils/sess.js"
Vue.use(VueRouter)
//引入路由文件

import list from "@/views/List.vue"
import Login from "@/views/Login.vue"

//设置守护卫士



export const routes = [
  //重定向
  {
    path: "/",
    redirect: "/login"
  },
  {
    path:"*",
    redirect:"/404",
  },
{
  path:"/404",
  isshow:false,
  component:()=>import("@/components/Rout404.vue"),
},
  //登录页面懒加载
  {
    path: "/login",
    component: Login,
  },

  /* 后台首页 */
  {
    path: "",
    component: list,
    //配置path
  
    children: [{
      path: '/home',
      meta:{title:"后台首页"},
      isshow:false,
      component: () => import("@/views/Home/Home.vue")
    }]
  },
  /* 订单管理 */
  {

    path: "",
    component: list,
    children: [{
      path: '/order',
      isshow:true,
      meta:{title:"订单管理"},
      component: () => import("@/views/Order/Order.vue")
    }]
  },
  /* 商品管理 */
  
  {
    path: "/shop",
    meta:{title:"商品管理"},
    redirect: "/shop-1",
    isshow:true,
    component: list,
    children: [
      {
        path: "/shop-1",
        isshow:true,
        isshow:true,
        meta:{title:"商品列表"},
        component: () => import("@/views/Shop/Shop-1.vue")
      },
      {
        path: "/shop-2",
        isshow:true,
        meta:{title:"商品添加"},
        component: () => import("@/views/Shop/Shop-2.vue")
      },
      {
        path: "/shop-3",
        meta:{title:"商品分类"},
        isshow:true,
        component: () => import("@/views/Shop/Shop-3.vue")
      },
      //个人中心
      {
        path: "/timg",
        meta:"个人中心",
        isshow:true,
        component: () => import("@/views/Shop/Timg.vue")
      }
    ]
  },
  /* 店铺管理 */
  {
    path: "",
    component: list,
    children: [{
      path: "/store",
      meta:{title:"店铺管理"},
      component: () => import("@/views/Store/Store.vue")
    }]
  },
  /* 账号管理 */
  {
    path: '/account',
    meta:{title:"账号管理",slotas:["super","nomorl"]},
    redirect: "/account-1",
    isshow:true,
    component: list,
    children: [
      {
        path: "/account-1",
        isshow:true,
        meta:{title:"账号列表",slotas:["super"]},

        component: () => import("@/views/Account/Account-1.vue")
      },
      
      {
        path: "/account-2",
        meta:{title:"添加账号",slotas:["super"]},
        isshow:true,
        component: () => import("@/views/Account/Account-2.vue")
      },
      {
        path: "/account-3",
        meta:{title:"修改密码",slotas:["super","nomorl"]},
        isshow:true,
        component: () => import("@/views/Account/Account-3.vue")

      },
    ]
  },
  /* 销售统计 */
  {
    path: "acc",
    component: list,
    redirect: "/acc-1",
    isshow:true,
    meta:{title:"销售统计",slotas:["super"]},
    children: [
      {
        path: "/acc-1",
        isshow:true,
        meta:{title:"商品统计",slotas:["super"]},
        component: () => import("@/views/Acc/Acc-1.vue")
      },
      {
        path: "/acc-2",
        isshow:true,
        meta:{title:"订单统计",slotas:["super"]},
        component: () => import("@/views/Acc/Acc-2.vue")
      },

    ]

  },
];

// 解决两次访问相同路由地址报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
 

const router = new VueRouter({
  routes
})

// 全局导航守卫
// 所有的路由都会先走守卫
// 添加导航守卫之后，不管是访问哪个路由，都会执行beforeEach回调函数中的代码
// 因为所有的路由，都会经过该导航守卫，所以，就可以在这个导航守卫的回调函数中
// 判断有没有登录了
router.beforeEach((to, from, next) => {
  // console.log('导航守卫在看门', to)
  // ...

  if (to.path === '/login') {
    // 如果访问的是login页面，直接放行，也就是任何人不管有没有登录
    // 都可以访问登录页面
    // 直接调用 next() 方法，表示：访问的是哪个页面，就展示这个页面的内容
    next()
  } else {
    // 访问的不是登录页面

    // 判断用户是否登录成功，
    // 1 当用户登录成功，直接调用 next() 方法放行
    // 2 当用户没有登录，应该调用 next('/login') 跳转到登录页面，让用户登录

    // 通过登录成功时候保存的token，来作为有没有登录成功的条件
    const token = sess.get('uid')
    if (token) {
      //到登录页
      
     next()
      // 有，登录成功
      
      
    } else {
      // 没有，登录失败
      next('/login')
    }
  }
})

export default router
